package uk.org.mygrid.eobesity.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;

/**
 * Methods for converting CSV files to SPSS and Stata format
 * using the R stats programme
 * @author Ian Dunlop
 */
public class RServeUtil {

    private static Logger logger = Logger.getLogger(RServeUtil.class);

    /**
     * Given a csv file, use R to create a spss data file
     * and code file
     * @param file - the csv file to be converted
     * @return a list containing the spss files
     */
    public List<File> createSPSSFiles(File file) {
        RConnection c;
        List<File> fileList = new ArrayList<File>();
        try {
            c = new RConnection();
            String[] split = file.getName().split("\\.");
            String name = split[0];
            File dataFile = new File(file.getParentFile(), name + "_spss_data.txt");
            File codeFile = new File(file.getParentFile(), name + "_spss_code.sps");
            c.voidEval("hse <-read.csv('" + file.getAbsolutePath() + "')");
            c.voidEval("library(foreign)");
            c.voidEval("write.foreign(hse, '" + dataFile.getAbsolutePath() + "'," + "'" + codeFile.getAbsolutePath() + "', package='SPSS')");
            fileList.add(dataFile);
            fileList.add(codeFile);
        } catch (RserveException e) {
            logger.info(e);
            System.out.println(e);
        }
        return fileList;
    }

    /**
     * Given a csv file, use R to create a stata data file
     * @param file - the csv file to be converted
     * @return a file containing the stata file
     */
    public File createStataFile(File file) {
        RConnection c;
        File dataFile = null;
        try {
            c = new RConnection();
            String[] split = file.getName().split("\\.");
            String name = split[0];
            dataFile = new File(file.getParentFile(), name + "_stata_data.dta");
            c.voidEval("hse <-read.csv('" + file.getAbsolutePath() + "')");
            c.voidEval("library(foreign)");
            c.voidEval("write.dta(hse, '" + dataFile.getAbsolutePath() + "')");
        } catch (RserveException e) {
            logger.info(e);
            System.out.println(e);
        }
        return dataFile;
    }

    public void doSomething() {
        RConnection c;
        String[] x = null;
        try {
            c = new RConnection();
            c.voidEval("hse <-read.csv('/Users/Ian/Downloads/march_test_4/hse05ai_selection.csv')");
            c.voidEval("library(foreign)");
            c.voidEval("write.foreign(hse, '/Users/Ian/scratch/spss_data.txt', '/Users/Ian/scratch/spss_code.sps', package='SPSS')");
            x = c.eval("names(hse)").asStrings();
            for (int i = 0; i < x.length; i++) {
                System.out.println(x[i]);
            }
        } catch (RserveException e) {
            System.out.println(e);
        } catch (REXPMismatchException e) {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
//        RServeUtil util = new RServeUtil();
//        util.doSomething();
    }
}
